'''
Author: daniel
Date: 2024-11-21 16:01:59
LastEditTime: 2024-11-21 17:14:57
LastEditors: daniel
Description: 
FilePath: /fastreid/svpbs/annotation_check.py
have a nice day
'''



from os.path import join, split, exists,  isdir,dirname,basename

import os 
import sys 
sys.path.append(dirname(os.getcwd()))
# os.chdir(dirname(os.getcwd()))


from glob import glob 


import matplotlib.pyplot as plt
import numpy as np

from tools.utils import * 


from tqdm import tqdm


root = '/root/exp/fastreid/jupyters/cpu084_jhe'
filter_csv_path =  '/root/exp/fastreid/jupyters/cpu084_jhe/20240401_event_bak/INOUT_20240401.csv.filter.csv'
# store_name = '蛙来哒'
# store_name = '月溪台'
# store_name = '阿娜尔'
# store_name = '金阁佬'
# store_name = '凑凑'
# store_name = '太二'
# store_name = 'steak'
# store_name = '满分'
# store_name = '啫火'
# store_name = '老碗会'
# store_name = '古膳记'
# store_name = '锦江悦'
store_name = '农耕记'








# command = "cat %s | grep %s | grep ^id"%(filter_csv_path,store_name)
command = "cat %s | grep %s "%(filter_csv_path,store_name)

print('command is %s'%(command))

lines, _ = run_shell_command(command)
lines = [line for line in lines.strip().split('\n')]


pids = [x.split(',')[0] for x in lines]
pids = set(pids)



annotation = load_json(join(root, 'labeling_result_20241029095720.json'))
print('annotation pid number is %d'%(len(annotation)))
ann_pid2label = {}



labels = set()
for k,v in annotation.items():
    pid = k.split('_')[0]
    if pid.startswith('id'):
        if v['label_result']['personType'] == 1 :
            labels.add(v['label_result']['personType'])
            ann_pid2label[pid] = v['label_result']['personType']




print('labels: {}'.format(labels))
ann_pids = set(ann_pid2label.keys())



print('number of annotation pid is %d \t pids extracted by corresponding store name from filter.csv is %d'%(len(ann_pids), len(pids)))
union = ann_pids & pids




print('union pid number is %d'%(len(union)))
# print(union)


#* vis union into a specific folder

send2annotation_root = '/root/exp/fastreid/logs/ImageTag_CR_zhongshan_wxhpoc_0401'

save_root =  '/root/exp/fastreid/logs/%s'%(store_name)

make_dir(save_root)
for unoin_pid in tqdm(union):
    unoin_pid_anns = glob(send2annotation_root+"/%s*.jpg"%unoin_pid)
    if len(unoin_pid_anns) >0:
        Image.open(unoin_pid_anns[0]).save(join(save_root,unoin_pid+'.jpg'))

        

    





